import React, { useMemo, useState, useEffect } from 'react';
import { show_yn, obj_int, ck_float, Info, obj_str, KSelect, KInput, ck_int, ck_empty, Dlg, loc, useKForm } from 'sui';
import { load_stuff } from './cmd';
import { kind_name } from 'buy/task/cmd';
import { sort_tech, yn_list } from 'test/base/cmd';
import { User } from 'cache';

const DlgBuy = ({ open, setOpen, hcmd, cur, is_acc }) => {
    const [map_stuff, mapStuff] = useState();
    const form = useKForm();

    useEffect(() => {
        if (!open) {
            return;
        }

        load_stuff(mapStuff);
    }, [open]);

    const getCol = useMemo(() => {
        if (!open) {
            return null;
        }

        const kind = obj_str(form.value, "kind");

        const tmp = [
            { sn: "sn", name: loc("l_363") },
        ];

        if (kind === "cera" || kind === "stell" ) {
            tmp.push({ sn: "ball", name: loc("l_714") });
            tmp.push({ sn: "num", name: loc("l_717") });
        }

        if (kind === "cage" || kind === "seal" ) {
            tmp.push({ sn: "ball", name: loc("l_714") });
        }

        return tmp;
    }, [open, form.value]);

    const getLst = useMemo(() => {
        if (!open) {
            return null;
        }

        const mt = obj_str(cur, "mt");
        const kname = kind_name();

        const lst = [
            { sn: "cage", name: obj_str(kname, "cage") },
            { sn: "seal", name: obj_str(kname, "seal") },
            { sn: "forge", name: obj_str(kname, "forge") },
            { sn: "lathe", name: obj_str(kname, "lathe") },
        ];

        if (mt === "T") {
            lst.push({ sn: "cera", name: obj_str(kname, "cera") });
        } else if (mt === "G") {
            lst.push({ sn: "stell", name: obj_str(kname, "stell") });
        }

        const ret = [];

        lst.forEach(d => {
            if (is_acc(`b${d.sn}`)) {
                ret.push(d);
            }
        });

        return ret;
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [cur, open]);

    const getStuff = useMemo(() => {
        if (!open) {
            return null;
        }

        const kind = obj_str(form.value, "kind");
        if (!kind) {
            return null;
        }

        return map_stuff[kind];
    }, [open, form.value, map_stuff]);

    const hok = () => {
        const ck = {
            pnum: (v) => ck_int(v),
            kind: (v) => ck_empty(v),
        };

        const kind = form.get("kind");

        if (kind === "cera" || kind === "stell" ) {
            ck["dw"] = (v) => ck_float(v);
        }

        if (kind === "cage") {
            ck["stuff"] = (v) => ck_empty(v);
        }

        if (kind === "seal") {
            ck["face"] = (v) => ck_empty(v);
            ck["yn"] = (v) => ck_empty(v);
        }

        if (kind === "forge" || kind === "lathe") {
            ck["std"] = (v) => ck_empty(v);
        }

        if (form.isErr(ck)) {
            return;
        }

        const tech = obj_str(cur, "sn");

        const cmd = {
            pnum: obj_int(form.value, "pnum"),
            kind: form.get("kind"),
            tech: tech,
            pname: User.uName(),
        };

        let desc = [];

        if (kind === "seal" || kind === "cage") {
            cmd["tech"] = sort_tech(tech);
        } else if (kind === "lathe") {
            cmd["tech"] = sort_tech(tech, true);

            const tmp = sort_tech(tech);
            cmd["forge"] = `${tmp}-${obj_str(cur, "ball")}`;
        } else if (kind === "forge") {
            const tmp = sort_tech(tech);
            cmd["tech"] = `${tmp}-${obj_str(cur, "ball")}`;
        }

        if (kind === "cera" || kind === "stell" ) {
            const dw = form.get("dw");
            cmd["dw"] = dw;

            desc.push(`${loc("l_873")}:${dw}`);
        }

        if (kind === "seal") {
            const face = form.get("face");
            const yn = form.get("yn");

            cmd["face"] = face;
            cmd["yn"] = yn;

            desc.push(`LOGO:${show_yn(yn)}`);
        }

        if (kind === "cage") {
            const stuff = form.get("stuff");
            cmd["stuff"] = stuff;

            const tmp = map_stuff[kind];
            const f = tmp.find(d => d.sn === stuff);
            if (f) {
                desc.push(`${loc("l_720")}:${obj_str(f, "name")}`);
            }
        }

        if (kind === "cera" || kind === "stell" || kind === "seal" || kind === "cage") {
            const ball = obj_str(cur, "ball");
            cmd["ball"] = ball;

            desc.push(`${loc("l_714")} : ${ball}`);
        }

        if (kind === "forge" || kind === "lathe" ) {
            cmd["std"] = form.get("std");
        }

        cmd["desc"] = desc.join(",");

        setOpen(false);
        hcmd(cmd);
    };

    const lst_std = [
        { sn: "both", name: loc("l_900") },
        { sn: "inner", name: loc("l_808") },
        { sn: "outer", name: loc("l_809") },
    ];

    const lst_face = [
        { sn: "both", name: "AB" },
        { sn: "A", name: "A" },
        { sn: "B", name: "B" },
    ];

    const getView = () => {
        const kind = form.get("kind");
        if (!kind) {
            return null;
        }

        if (kind === "cage") {
            return <KSelect label={loc("l_720")} name="stuff" form={form} lst={getStuff} className="mt-2"/>;
        }

        if (kind === "seal") {
            return (
                <>
                    <KSelect label={`AB${loc("l_814")}`} name="face" form={form} lst={lst_face} className="mt-2"/>
                    <KSelect label="Logo" name="yn" form={form} lst={yn_list()} className="mt-2"/>
                </>
            );
        }

        if (kind === "cera" || kind === "stell") {
            return <KInput label={loc("l_715")} name="dw" form={form}/>;
        }

        if (kind === "forge" || kind === "lathe" ) {
            return <KSelect label={loc("l_547")} name="std" form={form} lst={lst_std} className="mt-2"/>;
        }

        return null;
    };

    return (
        <Dlg open={open} hcancel={() => setOpen(false)} hcmd={hok} title={loc("l_552")}>
            <Info data={cur} col={getCol} className="mb-4"/>
            <KSelect label={loc("l_229")} name="kind" form={form} lst={getLst}/>
            {getView()}
            <KInput label={loc("l_127")} name="pnum" form={form}/>
        </Dlg>
    );
};

export {DlgBuy};
